function appendToken(request) {
    var t = getName(tokenName);
    if (t != null ) {
        request.setRequestHeader("token", getName(tokenName));
    }
}
function isInList(t, list) {
    var len = list.length;
    for (var i=0; i<len; ++i) {
        if (list[i] == t) {
            return true;
        }
    }
    return false;
}
function checkPower(t) {
    $.ajax ({
        type: "GET",
        url: ojurl + "/user/powers",
        dataType: "json",
        contentType: "application/json",
        responseType: "application/json",
        timeout: 10000,
        beforeSend: function(request) {
            appendToken(request);
        },
        success: function(result) {
            if (result.code != HttpStatus.SUCCESS) {
                localStorage.removeItem("name");
                localStorage.removeItem("token");
                location.href = "/front/index";
            }
            else {
                var powerList = result.data;
                if(isInList(t, powerList) == false) {
                    alert("你暂时没有权限进入此页面");
                    window.location.href = "/front/index";
                }
            }
        }
    })
}

// contest
function addContest(jsonData) {
    var data = new Object();
    data.data = jsonData;
    $.ajax({
        type: "POST",
        url : ojurl + "/admin/contest/add",
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(data),
        timeout: 10000,
        beforeSend: function(request) {
            appendToken(request);
        },
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
                alert("上传成功");
                window.location.href = "./contestList";
            }
            else {
                alert(result.message);
            }
        }
    })
}
function updateContest(jsonData) {
    var data = new Object();
    data.data = jsonData;
    $.ajax({
        type: "POST",
        url : ojurl + "/admin/contest/update",
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(data),
        timeout: 10000,
        beforeSend: function(request) {
            appendToken(request);
        },
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
                alert("修改成功");
                window.location.href = "./contestList";
            }
            else {
                alert(result.message);
            }
        }
    })
}
function addProblemToContest(jsonData, layer) {
    var data = new Object();
    data.data = jsonData;
    $.ajax({
        type: "POST",
        url : ojurl + "/admin/contest/problem/add",
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(data),
        timeout: 10000,
        beforeSend: function(request) {
            appendToken(request);
        },
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
                window.location.reload();
            }
            else {
                layer.alert(result.message);
            }
        }
    })
}
function deleteProblem(jsonData) {
    var data = new Object();
    data.data = jsonData;
    $.ajax({
        type: "POST",
        url : ojurl + "/admin/contest/problem/delete",
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(data),
        timeout: 10000,
        beforeSend: function(request) {
            appendToken(request);
        },
        success: function(result) {
            if (result.code != HttpStatus.SUCCESS) {
                alert(result.message);
            }
            else {
                window.location.reload();
            }
        }
    })
}
function addUser(jsonData) {
    var data = new Object();
    data.data = jsonData;
    $.ajax({
        type: "POST",
        url : ojurl + "/admin/contest/user/add",
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(data),
        timeout: 10000,
        beforeSend: function(request) {
            appendToken(request);
        },
        success: function(result) {
            if (result.code != HttpStatus.SUCCESS) {
                layer.alert(result.message);
            }
            else {
                window.location.reload();
            }
        }
    })
}
function addSchoolGroup(jsonData) {
    var data = new Object();
    data.data = jsonData;
    $.ajax({
        type: "POST",
        url : ojurl + "/admin/contest/user/group/add",
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(data),
        timeout: 10000,
        beforeSend: function(request) {
            appendToken(request);
        },
        success: function(result) {
            if (result.code != HttpStatus.SUCCESS) {
                window.location.reload();
                alert(result.message);
            }
            else {
                window.location.reload();
            }
        }
    })
}
function addUserFromOldContest(jsonData) {
    var data = new Object();
    data.data = jsonData;
    $.ajax({
        type: "POST",
        url : ojurl + "/admin/contest/user/other/add",
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(data),
        timeout: 10000,
        beforeSend: function(request) {
            appendToken(request);
        },
        success: function(result) {
            if (result.code != HttpStatus.SUCCESS) {
                alert(result.message);
            }
            else {
                window.location.reload();
            }
        }
    })
}
function deleteUser(jsonData) {
    var data = new Object();
    data.data = jsonData;
    $.ajax({
        type: "POST",
        url : ojurl + "/admin/contest/user/delete",
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(data),
        timeout: 10000,
        beforeSend: function(request) {
            appendToken(request);
        },
        success: function(result) {
            if (result.code != HttpStatus.SUCCESS) {
                alert(result.message);
            }
            else {
                window.location.reload();
            }
        }
    })
}
function submitContestUserEdit(postList) {
    var list = new Array();
    for (key in postList) {
        list.push(postList[key]);
    }
    var data = new Object();
    data.data = list;
    $.ajax({
        type: "POST",
        url : ojurl + "/admin/contest/user/update",
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(data),
        timeout: 10000,
        beforeSend: function(request) {
            appendToken(request);
        },
        success: function(result) {
            if (result.code != HttpStatus.SUCCESS) {
                alert(result.message);
            }
            else {
                window.location.reload();
                alert("修改成功");
            }
        }
    })
}
function getCode(solutionId, layer) {
    $.ajax({
        type: "GET",
        url: ojurl + "/admin/contest/code",
        timeout: 10000,
        data: {"solutionId": solutionId},
        beforeSend: function(request) {
            appendToken(request);
        },
        success: function(result) {
            layer.open({
                type: 1,
                title: '查看代码 '+solutionId,
                area: ['520px', '380px'],
                shade: 0,
                content: '<textarea style="margin: 1em; width: 480px; height: 250px">' + result.data.code + "</textarea>",
                btn: ['ok'], //只是为了演示
                yes: function(){
                  layer.closeAll();
                }
              });
        }
    })
}
function rejudge(list) {
    var data = new Object();
    data.data = list;
    $.ajax({
        type: "POST",
        url : ojurl + "/admin/contest/rejudge",
        dataType: "json",
        contentType: "application/json",
        timeout: 10000,
        data: JSON.stringify(data),
        beforeSend: function(request) {
            appendToken(request);
        },
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
                var plist = "[";
                for (var i of list) {
                    plist += String(i) + ",";
                }
                plist += "]";
                alert('重判 ' + plist + ' 成功');
            }
            else {
                alert("重判失败");
            }
        }
    })
}
function submitContestProblemEdit(contestId, postList) {
    var list = new Array();
    for (key in postList) {
        list.push(postList[key]);
    }
    var jsonData = new Object();
    jsonData.list = list;
    jsonData.contestId = String(contestId);
    var data = new Object();
    data.data = jsonData;
    console.log(data);
    $.ajax({
        type: "POST",
        url : ojurl + "/admin/contest/problem/update",
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(data),
        timeout: 10000,
        beforeSend: function(request) {
            appendToken(request);
        },
        success: function(result) {
            if (result.code != HttpStatus.SUCCESS) {
                alert(result.message);
            }
            else {
                window.location.reload();
                alert("修改成功");
            }
        }
    })
}




// user
function updateDeletedUser(jsonData) {
    var data = new Object();
    data.data = jsonData;
    $.ajax({
        type: "POST",
        url : ojurl + "/admin/user/prohibit",
        dataType: "json",
        contentType: "application/json",
        timeout: 10000,
        data: JSON.stringify(data),
        beforeSend: function(request) {
            appendToken(request);
        },
        success: function(result) {
          if (result.code == HttpStatus.SUCCESS) {
            layer.alert("用户 " + jsonData.userId + " 的是否禁止登陆已改为: " + jsonData.delete);
          }
          else {
            layer.alert("修改失败</br>" + result.message);
          }
        }
      })
}
function changePassword(jsonData, layer) {
    var data = new Object();
    data.data = jsonData;
    $.ajax({
        type: "POST",
        url : ojurl + "/admin/user/password/change",
        dataType: "json",
        contentType: "application/json",
        timeout: 10000,
        data: JSON.stringify(data),
        beforeSend: function(request) {
            appendToken(request);
        },
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
                layer.closeAll();
                alert("修改成功");
            }
            else {
                layer.alert(result.message);
            }
        }
    })
}
function getUserPowerList(name) {
    var powerList;
    $.ajax ({
        type: "GET",
        url: ojurl + "/admin/user/power/list",
        data: {"name": name},
        beforeSend: function(request) {
            appendToken(request);
        },
        async: false,
        timeout: 10000,
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
              powerList = result.data;
            }
        }
    })
    return powerList;
}
function changePower(jsonData) {
    var data = new Object();
    data.data = jsonData;
    $.ajax({
        type: "POST",
        url : ojurl + "/admin/user/power/change",
        dataType: "json",
        contentType: "application/json",
        timeout: 10000,
        data: JSON.stringify(data),
        beforeSend: function(request) {
            appendToken(request);
        },
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
                window.location.reload();
                alert("修改成功");
            }
            else {
                window.location.reload();
                alert(result.message);
            }
        }
    })
}
function showUserCode(solutionId) {
    $.ajax({
        type: "GET",
        url: ojurl + "/admin/user/code",
        timeout: 10000,
        data: {"solutionId": solutionId},
        beforeSend: function(request) {
            appendToken(request);
        },
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
                document.getElementById("code").value = result.data.code;
            }
            else {
                alert(result.message);
            }
        }
    })
}



//  problem control
function spjUpload(formData, jsonData) {
    var data = new Object();
    data.data = jsonData;
    $.ajax({
        url      : ojurl + "/admin/problem/data",
        type     : "post",
        timeout  : 600000,
        data     : formData,
        cache: false,
        contentType: false,
        processData: false,
        async: false,
        beforeSend : function(request){
          appendToken(request);
        },
        success  : function(result){
            if (result.code != HttpStatus.SUCCESS) {
                alert(result.message);
                return;
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          if (textStatus == 'timeout') {
            alert("上传数据超时");
            return;
          }
        }
      })
    $.ajax({
        url: ojurl + "/admin/problem/spj",
        type: "post",
        dataType: "json",
        contentType: "application/json",
        timeout: 10000,
        data: JSON.stringify(data),
        beforeSend: function(request) {
            appendToken(request);
        },
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
                alert("上传成功");
                location.href = "./problemList";
            }
            else {
                alert("SPJ源码上传失败： " + result.message);
            }
        }
    })
}